// Copyright 2007-2009, Stanford University. All Rights Reserved.
// Copyright 2010-2011, Dependable Systems Lab, EPFL. All Rights Reserved.
// Copyright 2012, Google Inc. All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimers.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimers
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the names of the klee Team, Stanford University, nor the
// names of its contributors may be used to endorse or promote
// products derived from the software without specific prior
// written permission. Neither the names of the Dependable Systems
// Laboratory, EPFL, nor the names of its contributors may be used to
// endorse or promote products derived from the software without prior
// written permission. Neither the names of Google Inc. nor the names of
// its contributors may be used to endorse or promote products derived from
// the software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef SRC_UTIL_BITS_H_
#define SRC_UTIL_BITS_H_

#include "llvm/Support/DataTypes.h"

namespace seven_summits {
namespace util {
namespace bits32 {
  // @pre(0 <= N <= 32)
  // @post(retval = max([truncateToNBits(i,N) for i in naturals()]))
  inline unsigned maxValueOfNBits(unsigned N) {
    if (N==0)
      return 0;
    return ((unsigned) -1) >> (32 - N);
  }

  // @pre(0 < N <= 32)
  inline unsigned truncateToNBits(unsigned x, unsigned N) {
    return x&(((unsigned) -1) >> (32 - N));
  }

  inline unsigned withoutRightmostBit(unsigned x) {
    return x&(x-1);
  }

  inline unsigned isolateRightmostBit(unsigned x) {
    return x&-x;
  }

  inline unsigned isPowerOfTwo(unsigned x) {
    if (x==0) return 0;
    return !(x&(x-1));
  }

  // @pre(withoutRightmostBit(x) == 0)
  // @post((1 << retval) == x)
  inline unsigned indexOfSingleBit(unsigned x) {
    unsigned res = 0;
    if (x&0xFFFF0000) res += 16;
    if (x&0xFF00FF00) res += 8;
    if (x&0xF0F0F0F0) res += 4;
    if (x&0xCCCCCCCC) res += 2;
    if (x&0xAAAAAAAA) res += 1;
    return res;
  }

  inline unsigned indexOfRightmostBit(unsigned x) {
    return indexOfSingleBit(isolateRightmostBit(x));
  }
}  // namespace bits32

namespace bits64 {
  // @pre(0 <= N <= 32)
  // @post(retval = max([truncateToNBits(i,N) for i in naturals()]))
  inline uint64_t maxValueOfNBits(unsigned N) {
    if (N==0)
      return 0;
    return ((uint64_t) (int64_t) -1) >> (64 - N);
  }

  // @pre(0 < N <= 64)
  inline uint64_t truncateToNBits(uint64_t x, unsigned N) {
    return x&(((uint64_t) (int64_t) -1) >> (64 - N));
  }

  inline uint64_t withoutRightmostBit(uint64_t x) {
    return x&(x-1);
  }

  inline uint64_t isolateRightmostBit(uint64_t x) {
    return x&-x;
  }

  inline uint64_t isPowerOfTwo(uint64_t x) {
    if (x==0) return 0;
    return !(x&(x-1));
  }

  // @pre((x&(x-1)) == 0)
  // @post((1 << retval) == x)
  inline unsigned indexOfSingleBit(uint64_t x) {
    unsigned res = bits32::indexOfSingleBit((unsigned) (x | (x>>32)));
    if (x&((uint64_t) 0xFFFFFFFF << 32))
  res += 32;
    return res;
  }

  inline uint64_t indexOfRightmostBit(uint64_t x) {
    return indexOfSingleBit(isolateRightmostBit(x));
  }
} // namespace bits64

} // namespace util
} // namespace seven_sunmits

#endif  // SRC_UTIL_BITS_H_
